<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Session;
use Validator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class AdminLoginController extends Controller
{
    public function login(Request $request){
        return view('admin.login');
    }

    public function check(Request $request){
        if($request->isMethod('post')){
            $data = $request->all();

            $validator = Validator::make($request->all(), [
                'username' => 'required|alpha',
                'password' => 'required',
                'captcha'  => 'required|captcha'
            ]);
            if (!$validator->passes()) {
                $messages = $validator->messages();
                $str='';
                foreach ($messages->all() as $message)
                {
                    $str.=$message;
                }
                return showMsg(0,$str);
            }
            //验证
            $res = Auth::attempt(['username'=>$data['username'],'password'=>$data['password']]);
            if(!$res){
                return showMsg(0,'用户信息不正确');
            }
            return showMsg(200,'登录成功');
        }else{
            return showMsg(0,'请求方式不合法');
        }

    }


}
